package com.tongjie.hotelbooking.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    private static final String TOKEN_KEY = "X-Auth-Token";

    @Bean
    public Docket newsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tongjie.hotelbooking"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(this.globalOperationParameters());
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Archangel")
                .description("HotelBooking接口说明文档")
                .contact(new Contact("ADMIN", null, ""))
                .license("tongjie.com").licenseUrl("#")
                .version("1.0-SNAPSHOT")
                .build();
    }

    private List<Parameter> globalOperationParameters() {
        Parameter tokenParameter = new ParameterBuilder().name(TOKEN_KEY).description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        return Arrays.asList(tokenParameter);
    }
}
